package com.liming.admin.controller;

import com.liming.auth.client.MenuClient;
import com.liming.common.pojo.dto.authinfo.AddOrUpdateMenuDTO;
import com.liming.common.pojo.vo.authinfo.MenuVO;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liming
 * @date 2025/2/16
 */
@Slf4j
@RestController
@RequestMapping("/admin/menu")
@AllArgsConstructor
@Tag(name = "菜单接口-Web")
public class MenuController {

    private final MenuClient menuClient;

    @Operation(
            summary = "树 - 菜单树",
            responses = {
                    @ApiResponse(responseCode = "1", description = "成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MenuVO.class))
                    })
            }
    )
    @GetMapping
    public LMResult<List<MenuVO>> tree() {
        return LMResult.success(menuClient.tree());
    }

    @Operation(
            summary = "列表 - 角色对应菜单权限",
            responses = {
                    @ApiResponse(responseCode = "1", description = "成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = Long.class))
                    })
            }
    )
    @GetMapping("role/{id}")
    public LMResult<List<Long>> roleMenu(@PathVariable Long id) {
        return LMResult.success(menuClient.roleMenu(id));
    }

    @Operation(summary = "新增 - 菜单")
    @PostMapping
    public LMResult<Boolean> add(@RequestBody AddOrUpdateMenuDTO addOrUpdateMenuDTO) {
        return LMResult.success(menuClient.add(addOrUpdateMenuDTO));
    }

    @Operation(summary = "更新 - 菜单")
    @PutMapping
    public LMResult<Boolean> update(@RequestBody AddOrUpdateMenuDTO addOrUpdateMenuDTO) {
        return LMResult.success(menuClient.update(addOrUpdateMenuDTO));
    }

    @Operation(summary = "更新 - 角色菜单权限")
    @PutMapping("role/{id}")
    public LMResult<Boolean> updRoleMenu(@PathVariable Long id, @RequestBody List<Long> menuIds) {
        return LMResult.success(menuClient.updRoleMenu(id, menuIds));
    }

    @Operation(summary = "删除 - 菜单")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        return LMResult.success(menuClient.delete(id));
    }

}
